// MUD Programming
// Ron Penton
// (C)2003
// BasicLibTime.cpp - This file contains all time-based classes and functions
#include <time.h>
#include <sys/time.h>
#include "BasicLibTime.h"
#include "BasicLibString.h"



namespace BasicLib{


    // ============================================================
    // These functions get a time value. The Actual meaning of this 
    // time is undefined; it is only meant to be relative.
    // ============================================================
    long long GetTimeMS(){
        struct timeval t;
        long long s;
        
        // get the time of day
        gettimeofday( &t, 0 );

        // calculate the number of milliseconds represented by the seconds
        s = t.tv_sec;
        s *= 1000;

        // calculate the milliseconds now
        s += (t.tv_usec / 1000);

        // return the value
        return s;
    }


    long long GetTimeS(){
        return GetTimeMS() / 1000;
    }

    long long GetTimeM(){
        return GetTimeMS() / 60000;
    }

    long long GetTimeH(){
        return GetTimeMS() / 3600000;
    }
    

    // ============================================================
    // This prints a timestamp in 24 hours hh:mm:ss format
    // ============================================================
    std::string TimeStamp(){
        char str[9];

        // get the time, and convert it to struct tm format
        time_t a = time(0);
        struct tm* b = localtime( &a );

        // print the time to the string
        strftime( str, 9, "%H:%M:%S", b );

        return str;
    }


    // ============================================================
    // This prints a datestamp in YYYY:MM:DD format
    // ============================================================
    std::string DateStamp(){
        char str[11];

        // get the time, and convert it to struct tm format
        time_t a = time(0);
        struct tm* b = localtime( &a );

        // print the time to the string
        strftime( str, 11, "%Y.%m.%d", b );

        return str;
    }


    Timer::Timer(){
        m_starttime = 0;
        m_inittime = 0;
    }


    void Timer::Reset( long long p_timepassed ){
        m_starttime = p_timepassed;
        m_inittime = GetTimeMS();
    }

    long long Timer::GetMS()
    {
        // return the amount of time that has elapsed since the timer
        // was initialized, plus whatever starting time the timer was given.
        return (GetTimeMS() - m_inittime) + m_starttime;
    }

    long long Timer::GetS()
    {
        return GetMS() / 1000;
    }

    long long Timer::GetM()
    {
        return GetMS() / 60000;
    }

    long long Timer::GetH()
    {
        return GetMS() / 3600000;
    }

    long long Timer::GetD()
    {
        return GetMS() / 86400000;
    }
    
    long long Timer::GetY()
    {
        return GetD() / 365;
    }

    std::string Timer::GetString()
    {
        std::string str;
        long long y = GetY();
        long long d = GetD() % 365;
        long long h = GetH() % 24;
        long long m = GetM() % 60;

        if( y > 0 )
            str += tostring( y ) + " years, ";
        if( d > 0 )
            str += tostring( d ) + " days, ";
        if( h > 0 )
            str += tostring( h ) + " hours, ";
        
        str += tostring( m ) + " minutes";

        return str;
    }


} // end namespace BasicLib
